package com.zoke.daycarpool.ui;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.zoke.daycarpool.R;
import com.zoke.daycarpool.adapter.ChannelAdapter;
import com.zoke.daycarpool.base.BaseUi;
import com.zoke.daycarpool.config.CarpoolConfig;
import com.zoke.daycarpool.config.CarpoolParamStore;
import com.zoke.daycarpool.config.UrlBank;
import com.zoke.daycarpool.domain.ChannelBeen;
import com.zoke.daycarpool.domain.Token;
import com.zoke.daycarpool.domain.UserBeen;
import com.zoke.daycarpool.domain.UserInstense;
import com.zoke.daycarpool.fragment.MainFragment;
import com.zoke.daycarpool.parser.ChannelParser;
import com.zoke.daycarpool.util.LogTest;
import com.zoke.support.http.HttpStores;
import com.zoke.support.http.ZokeParams;
import com.zoke.support.image.ShowImagesAdapter.AdaterListener;
import com.zoke.support.utils.PersistTool;
import com.zoke.support.view.XListView;

/**
 * 频道列表
 * 
 * @author JackWu
 * @version
 * @date 2014-12-30
 * @_QQ_ 651319154
 */
public class Channels extends BaseUi implements AdaterListener, OnClickListener {
	private XListView mListView;
	private List<ChannelBeen> mList = new ArrayList<ChannelBeen>();
	private ChannelAdapter mAdapter;

	private View mHeadView;//
	private Button mBtnAdd;
	private UserBeen mUser;
	//
	public static final int resultChannel = 1200;
	/**  **/
	public static final String channelId = "_channelId";
	public static final String channelNameForStart = "_channelNamestart";
	public static final String channelNameForEnd = "_channelNameend";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_channels);
		mTitleManager.setTitle("频道列表");
		mTitleManager.setLeftIcon(R.drawable.title_back);
		mEnableSwipeRight = true;
		mAdapter = new ChannelAdapter(Channels.this, mList, this);
		mUser = UserInstense.getInstense(Channels.this).getUser();
		findView();
		mListView.setAdapter(mAdapter);
		doEvent();
		fetchChannels();
	}

	/** 获取频道列表 get **/
	private void fetchChannels() {
		if (mUser == null)
			return;
		ZokeParams param = CarpoolParamStore.getDefaultParam(mHashCode);
		param.setUrl(UrlBank.doChanles);
		param.put("sid", "" + mUser.getUserId());
		param.setUseCache(true);
		HttpStores.getInstense(getApplicationContext()).get(param);
	}

	@Override
	public void onSuccess(ZokeParams out) {
		super.onSuccess(out);
		String result = (String) out.getResult();
		LogTest.tag("result=" + result);
		ChannelParser mParser = new ChannelParser();
		try {
			List<ChannelBeen> list = mParser.parseJson(result);
			if (mParser.getCode() == 0) {
				Token.getTokener().updateToken(mParser.getToken());
				if (list != null && list.size() != 0) {
					mList.clear();
					mList.addAll(list);
					mAdapter.notifyDataSetChanged();
				}
				return;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onFails(ZokeParams out) {
		super.onFails(out);
		// ZokeErro erro = (ZokeErro) out.getResult();
		// ZokeToast.show(getApplicationContext(), "" + erro.getErrorNo());
		// ZokeToast.showNetFailed(getApplicationContext());
	}

	@Override
	protected void findView() {
		mHeadView = LayoutInflater.from(getApplicationContext()).inflate(
				R.layout.include_channel_bottom, null);
		mBtnAdd = (Button) mHeadView.findViewById(R.id._addroute);
		mListView = (XListView) findViewById(R.id._listView);
		mListView.addFooterView(mHeadView);
	}

	@Override
	protected void doEvent() {
		mListView.setPullLoadEnable(false);
		mListView.setPullRefreshEnable(false);
		mBtnAdd.setOnClickListener(this);
	}

	@Override
	public void onAdapterItemClick(int position, Object obj) {
		ChannelBeen cb = mList.get(position);
		// 要id和名字存起来
		Intent intent = new Intent();
		intent.putExtra(channelNameForStart, "" + cb.getStart());
		intent.putExtra(channelNameForEnd, cb.getDest());
		intent.putExtra(channelId, cb.getChId());
		setResult(resultChannel, intent);
		PersistTool.saveString(MainFragment.ChannelNameForStart,
				intent.getStringExtra(Channels.channelNameForStart));// 分开存
		PersistTool.saveString(MainFragment.ChannelNameForEnd,
				intent.getStringExtra(Channels.channelNameForEnd));// 分开存
		PersistTool.saveString(MainFragment.ChannelId, cb.getChId() + "");
		// 发布搜地址时有用
		PersistTool.saveString(MainFragment.startCityCode, cb.getStartcode());
		PersistTool.saveString(MainFragment.endCityCode, cb.getDestcode());
		this.onBackPressed();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id._addroute:
			mIntent = new Intent(this, AddRoute.class);
			startUI(mIntent);
			break;

		default:
			break;
		}
	}

}
